package com.zxsc.zxmusic.fragment;

import java.util.ArrayList;
import java.util.List;

import com.xlist.XListView;
import com.zxsc.zxmusic.R;
import com.zxsc.zxmusic.adapter.AdapterDialog;
import com.zxsc.zxmusic.db.SongDb;
import com.zxsc.zxmusic.model.SongMenuModel;
import com.zxsc.zxmusic.utils.UserUtils;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class DialogFragment extends android.support.v4.app.DialogFragment{
	private ListView listView;
	private TextView txtTitle;
	private AdapterDialog adapter;
	OnSelMenuLister lister;
	private List<SongMenuModel> menus;
	
	@Override
	@Nullable
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
		View view=LayoutInflater.from(getActivity()).inflate(R.layout.fragment_dialog, null);
		initView(view);
		return view;
	}

	private void initView(View view) {
		menus=new ArrayList<>();
		listView=(ListView) view.findViewById(R.id.dialog_list);
		txtTitle=(TextView) view.findViewById(R.id.dialog_title);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				SongMenuModel info=menus.get(position);
				if(info!=null && lister!=null){
					lister.onMenu(info);
				}
			}
		});

		
		
		
		getMneuList();
	}
	
	
	private void upDateList(){
		if(adapter==null){
			adapter=new AdapterDialog(getActivity(), menus);
			listView.setAdapter(adapter);
		}
		adapter.notifyDataSetChanged();
	}
	
	private void getMneuList(){
		List<SongMenuModel> list=SongDb.getAllSongMenu(getActivity(), UserUtils.getInstance().getUserId());
		if(list!=null && list.size()>0){
			menus.clear();
			menus.addAll(list);
			upDateList();
		}
	}
	
	
	public void setOnSelMenuLister(OnSelMenuLister lister){
		this.lister=lister;
	}
	public interface OnSelMenuLister{
		public void onMenu(SongMenuModel menu);
	}

}
